/*
 * BaseEnemy.cpp
 *
 *  Created on: 04/02/2013
 *      Author: eusyar
 */

#include "game/entities/BaseEnemy.h"
#include "game/entities/EatsStage.h"
#include "util/Helpers.h"
#include <iostream>

using namespace eats_helpers;
using namespace eats_core;
using namespace std;

BaseEnemy::BaseEnemy(string canonical_name, string id, SDL_Surface * working, string animation_path, int _posX, int _posY,int maxframe = 1, int tileSize = 320, int path = 0) : GameObject(working, _posX, _posY)
{
	_canonical_name = canonical_name;
	_id = id;
	_animation_path = animation_path;

	_heath = 100;
	_goldReward = 1;

	_working_surface = working;

	_tileSize = tileSize;
	_maxFrames = maxframe;
	_path = path;

	setX(_posX);
	setY(_posY);

}

BaseEnemy::~BaseEnemy()
{

}

bool BaseEnemy::loadAndEnableMoviment() {

	_moviment = new Moviment(getX(), getY());
	_moviment->_canonical_name = "Movimento do matusquela";

	getMovimentRoute(_path);

	addComponent(_moviment);
	enableComponent(_moviment);

	return true;
}

bool BaseEnemy::loadAndEnableAnimation()
{

	_animation = new Animation(_working_surface, _animation_path.append( PathHelper::resourceResolution() ).append(".png"),
			getX() , getY(), false, _maxFrames, _tileSize);
	string new_canonical_name = _canonical_name;
	string new_id = _id;
	_animation -> _canonical_name  = new_canonical_name.append("_animation");
	_animation -> _id = new_id.append("_animation");
	addComponent(_animation);
	enableComponent(_animation);

	return true;

}

bool BaseEnemy::loadAndEnableSound()
{
	_sound = new Sound_("/opt/attacks/resources/ws.wav", false);
	_sound->_canonical_name = "SOM do matusquela";

	addComponent(_sound);
	enableComponent(_sound);

	return true;

}

bool BaseEnemy::setUp(){
	bool ret = true;
	ret = ret && loadAndEnableAnimation();
	ret = ret && loadAndEnableMoviment();
	//ret = ret && loadAndEnableSound();
	ret = ret && GameObject::setUp();
	ret = ret && onSetUp();
	return ret;
}

bool BaseEnemy::update()
{
	if( _moviment->getFinalPosition().x == position->x && _moviment->getFinalPosition().y == position->y ){
		EatsStage * temp  = dynamic_cast<EatsStage*>(GameConteiner::getInstance()->getGameManager()-> _current_state);
		temp->destroyNemlinium(1);
		_isAlive = false;
	}
	return GameObject::update();
}

int BaseEnemy::sufferAttack(int damage)
{
	_heath -= damage;
	if(_heath <= 0)
	{
		_isAlive = false;
		//_sound->Play_Sound();
		return _goldReward;
	}
	return 0;
}

